import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class Test13 extends StatefulWidget{
    _TestState createState() => _TestState();
}

class _TestState extends State<Test13>{

    List<Offset> _points = <Offset>[];

    @override
    Widget build(BuildContext context) {

        return new Scaffold(
            // appBar: AppBar(
            //     title: Text("画板"),
            //     centerTitle: true,
            // ),
            body: Container(
                // width: 100.0,
                // height: 100.0,
                margin: EdgeInsets.all(8.0),
                decoration: BoxDecoration(
                    border: Border.all(width: 1.0,color: Colors.red)
                ),
                child: Stack(
                    children: <Widget>[
                        Image.network(
                            'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1564142586485&di=7f6342a02863689c83aa7b8c34109ea9&imgtype=0&src=http%3A%2F%2F5b0988e595225.cdn.sohucs.com%2Fimages%2F20190611%2F7305902eaf0c481f99df8a9fdc75423c.jpeg',
                            fit: BoxFit.fill,
                        ),
                        GestureDetector(
                            onPanUpdate: (DragUpdateDetails details){
                                // print(details);
                                RenderBox referenceBox = context.findRenderObject();
                                Offset localPosition = referenceBox.globalToLocal(details.globalPosition);
                                setState(() {
                                    _points = List.from(_points)..add(localPosition);
                                });
                            },
                            onPanEnd: (DragEndDetails details){
                                _points.add(null);
                            },
                        ),
                        CustomPaint(painter: SignaturePainter(_points),),
                        
                    ],
                ),
            )
        );
    }
}

class SignaturePainter extends CustomPainter{

    SignaturePainter(this.points);
    final List<Offset> points;

    void paint(Canvas canvas,Size size){
        print(points);
        Paint paint = Paint()
            ..color = Colors.red
            ..strokeCap = StrokeCap.round
            ..strokeWidth = 5.0
            ..strokeJoin = StrokeJoin.bevel;
        for(int i= 0;i<points.length -1;i++){
            if(points[i] != null && points[i+1] != null)
            canvas.drawLine(points[i], points[i+1], paint);
        }
    }

    bool shouldRepaint(SignaturePainter other){
        return other.points != points;
    }

}
